package cn.sciento.farm.devicemanager.gateway.uplink.service.fertilizer.inject;

import cn.sciento.farm.devicemanager.dao.entity.DevFertilizerPipe;
import cn.sciento.farm.devicemanager.dao.model.fertilizer.InjectFertilizer;
import cn.sciento.farm.devicemanager.gateway.model.uplink.fertilizer.inject.*;
import cn.sciento.farm.devicemanager.gateway.uplink.service.fertilizer.InjectFertilizerDisposeService;
import cn.sciento.farm.devicemanager.service.InjectFertilizerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class InjectFertilizerDataDisposeService implements InjectFertilizerDisposeService {

    @Autowired
    InjectFertilizerService injectFertilizerService;

    @Override
    public void handleStatus(InjectFertilizerStatus status) {
        InjectFertilizer injectFertilizer = injectFertilizerService.getByEui(status.getEui());
        if (injectFertilizer == null) {
            return;
        }
        injectFertilizer.setEc(status.getEc());
        injectFertilizer.setPh(status.getPh());
        injectFertilizer.setInstantFlow(status.getWaterPipe().getInstantFlow());
        List<DevFertilizerPipe> pipeList = injectFertilizer.getPipeList();
        List<InjectFertilizerStatus.PipeStatus> pipeStatusList = status.getFatPipe().getVariable();
        for (DevFertilizerPipe pipe : pipeList) {
            if (pipe.getPipeNumber() <= pipeStatusList.size()) {
                InjectFertilizerStatus.PipeStatus pipeStatus = pipeStatusList.get(pipe.getPipeNumber() - 1);
                pipe.setInstantFlow(pipeStatus.getInstantFlow());
                pipe.setPressure(pipeStatus.getPressure());
                pipe.setRatio(pipeStatus.getRatio());
            }
        }
        injectFertilizerService.updateById(injectFertilizer);
    }

    @Override
    public void handleWorkStatus(InjectFertilizerWorkStatus status) {
        InjectFertilizer injectFertilizer = injectFertilizerService.getByEui(status.getEui());
        if (injectFertilizer == null) {
            return;
        }
        // 不更新
        injectFertilizer.setOnline(null);
        injectFertilizer.setPipeAmount(null);
        injectFertilizer.setAreaId(null);
        injectFertilizer.setRemoteMode(null);
        injectFertilizer.setStatus(null);

        injectFertilizer.setWorkMode(status.getMode());
        injectFertilizer.setSchedule(status.getSchedule());

        injectFertilizer.setConsumption(status.getWaterPipe().getConsumption());
        List<DevFertilizerPipe> pipeList = injectFertilizer.getPipeList();
        List<InjectFertilizerWorkStatus.PipeStatus> pipeStatusList = status.getFatPipe().getVariable();
        for (DevFertilizerPipe pipe : pipeList) {
            if (pipe.getPipeNumber() <= pipeStatusList.size()) {
                InjectFertilizerWorkStatus.PipeStatus pipeStatus = pipeStatusList.get(pipe.getPipeNumber() - 1);
                pipe.setConsumption(pipeStatus.getConsumption());
                pipe.setTotalRatio(pipeStatus.getTotalRatio());
            }
        }
        injectFertilizerService.updateById(injectFertilizer);
    }

    @Override
    public void handleError(InjectFertilizerError error) {

    }

    @Override
    public void handleAck(InjectFertilizerAck ack) {
        InjectFertilizer injectFertilizer = injectFertilizerService.getByEui(ack.getEui());
        if (injectFertilizer == null) {
            return;
        }
        InjectFertilizer fertilizer = new InjectFertilizer();
        fertilizer.setId(injectFertilizer.getId());
        fertilizer.setWorkMode(ack.getMode());
        fertilizer.setStatus(ack.getActivate()?1:0);
        injectFertilizerService.updateById(fertilizer);

        // 处理控制逻辑


    }

    @Override
    public void handleSys(InjectFertilizerSys sys) {
        InjectFertilizer injectFertilizer = injectFertilizerService.getByEui(sys.getEui());
        if(injectFertilizer == null){
            return;
        }
        injectFertilizerService.updateMode(injectFertilizer.getId(),sys.getRemoteMode() ? 1 : 0);
    }

    @Override
    public void handleOnline(String eui, int status) {
        InjectFertilizer injectFertilizer = injectFertilizerService.getByEui(eui);
        if(injectFertilizer == null){
            return;
        }
        injectFertilizer.setOnline(status);
        injectFertilizerService.updateById(injectFertilizer);
    }


}
